package com.gxa.service.impl;

import com.gxa.common.JsonResult;
import com.gxa.common.PageRecv;
import com.gxa.common.R;
import com.gxa.mapper.DocumentMapper;
import com.gxa.mapper.DocumentaryMapper;
import com.gxa.mapper.EmployeeMapper;
import com.gxa.pojo.Dept;
import com.gxa.pojo.Document;
import com.gxa.pojo.Documentary;
import com.gxa.pojo.Employee;
import com.gxa.service.DocumentaryService;
import com.gxa.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 张剑宇
 * Date: 2021/12/02 18:38
 * Description:
 * Version: V1.0
 */
@Service
public class DocumentaryServiceImpl implements DocumentaryService{
    @Autowired(required = false)
    private DocumentaryMapper documentaryMapper;
    @Autowired(required = false)
    private EmployeeMapper employeeMapper;
    @Autowired(required = false)
    private DocumentMapper documentMapper;
    @Override
    public JsonResult findDocumentaryList(PageRecv<Documentary> pageRecv) {
        //对查询页面参数计算
        Integer currentPage=pageRecv.getCurrentPage();
        Integer pageSize=pageRecv.getPageSize();
        Integer limit1=(currentPage-1)*pageSize;
        pageRecv.setCurrentPage(limit1);
        //获取查询条件实体
        Documentary documentary=pageRecv.getData();
        //调用mapper方法，返回查询结果list
        List<Documentary> list=documentaryMapper.findDocumentaryListMapper(pageRecv);
        Integer count =documentaryMapper.findDocumentaryListCountMapper(pageRecv);
        //查询每条跟单记录的附件数量
        for (Documentary doc:list
             ) {
            Integer count1=documentMapper.countDocumentMapper((int)doc.getDocumentary_id());
            doc.setDocument_number(count1);
        }

        //将list和count条数塞入r,响应成功返回


        JsonResult r=new JsonResult();
        r.setCode(200);
        r.setMsg("查询成功");

        r.setCount(count);

        r.setData(list);

        return r;
    }

    //高级搜索
    @Override
    public JsonResult<Documentary> searchDocumentaryListByCondition(PageRecv<Documentary> pageRecv) {
        //对查询页面参数计算
        Integer currentPage=pageRecv.getCurrentPage();
        Integer pageSize=pageRecv.getPageSize();
        Integer limit1=(currentPage-1)*pageSize;
        pageRecv.setCurrentPage(limit1);
        //获取查询条件实体
        Documentary documentary=pageRecv.getData();
        //调用mapper方法，返回查询结果list
        List<Documentary> list=documentaryMapper.searchDocumentaryListMapper(pageRecv);
        Integer count=documentaryMapper.searchDocumentaryListCountMapper(pageRecv);
        //查询每条跟单记录的附件数量
        for (Documentary doc:list
                ) {
            Integer count1=documentMapper.countDocumentMapper((int)doc.getDocumentary_id());
            doc.setDocument_number(count1);
        }

        //将list和count条数塞入r,响应成功返回
        JsonResult r=new JsonResult();
        r.setCode(200);
        r.setCount(count);

        r.setMsg("查询成功");
        r.setData(list);

        return r;
    }

    //搜索
    @Override
    public JsonResult<Documentary> searchDocumentaryList(String condition,Integer pageSize,Integer currentPage,String employee_name) {
        //对查询页面参数计算

        Integer limit1=(currentPage-1)*pageSize;

        //调用mapper方法，返回查询结果list


        List<Documentary> list=documentaryMapper.searchDocumentaryListLowMapper(limit1,pageSize, condition,employee_name);
        int count=documentaryMapper.searchDocumentaryListLowCountMapper(condition,employee_name);
        //查询每条跟单记录的附件数量
        for (Documentary doc:list
                ) {
            Integer count1=documentMapper.countDocumentMapper((int)doc.getDocumentary_id());
            doc.setDocument_number(count1);
        }
        //将list和count条数塞入r,响应成功返回
        JsonResult r=new JsonResult();
        r.setCode(200);
        r.setCount(count);
        r.setMsg("查询成功");
        r.setData(list);

        return r;
    }

    @Override
    public R countDocumentaryByEmployee(String start_time,String stop_time) {
        List<Employee> empList=documentaryMapper.queryEmployee();
//        Integer count=empList.size();
        Map<String,Integer> result=new HashMap<>();
        for (Employee employee:empList
             ) {
            String employee_name=employee.getEmployee_name();
            Integer count1=documentaryMapper.countDocumentaryByEmployee(employee_name,start_time,stop_time);
            result.put(employee_name,count1);
        }
        R r= new R();
        r.setCode(200);
        r.setData(result);
        r.setMsg("查询成功");
        return r;
    }

    @Override
    public R countDocumentaryByDept(String start_time,String stop_time) {

        Map<String,Integer> result=new HashMap<>();
        List<Dept> deptList=documentaryMapper.queryDept();
        for (Dept dept:deptList
                ) {

            String dept_name=dept.getDept_name();
            Integer count1=documentaryMapper.countDocumentaryByDept(dept_name,start_time,stop_time);
            result.put(dept_name,count1);
        }
        R r= new R();
        r.setCode(200);
        r.setData(result);
        r.setMsg("查询成功");
        return r;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteDocumentary(Integer documentary_id) {
        documentaryMapper.deleteDocumentaryMapper(documentary_id);
        R r=new R();
        r.setCode(200);
        r.setMsg("删除成功！");
        return r;

    }

    @Override
    public R deleteDocumentaryByCustomerId(Integer customer_id) {
        List<Integer> list=documentaryMapper.findDocumentaryIdByCustomerId(customer_id);
        for (Integer documentary_id:list
             ) {
            documentaryMapper.deleteDocumentaryMapper(documentary_id);
            List<Document> documentlist=documentMapper.queryDocumentMapper(documentary_id);
            for (Document document: documentlist
                 ) {
                documentMapper.deleteDocumentMapper((int)document.getDocument_id());
            }

        }
        R r=new R();
        r.setMsg("删除成功");
        r.setCode(200);
        return  r;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addDocumentary(Documentary documentary) {
        documentaryMapper.addDocumentaryMapper(documentary);

        R r=new R();
        r.setCode(200);
        r.setData(documentary.getDocumentary_id());
        r.setMsg("添加成功！");
        return r;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateDocumentary(Documentary documentary) {
        documentaryMapper.updateDocumentaryMapper(documentary);
        R r=new R();
        r.setCode(200);
        r.setMsg("修改成功！");
        return r;
    }




}
